<script type="text/javascript" src="<?php echo JURI::root(); ?>js/dialog/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript">
    jQuery(function(){

        // Dialog
        jQuery('#dialog').dialog({
            autoOpen: false,
            width: 300,
            height: 200,
            open: function(){
                jQuery("#pick_up_date_obj").val(jQuery("#pick-up-date_"+jQuery("#pick_up_date_member").val()).find("span").html());
                jQuery(this).dialog("open");
            },
            buttons: {
                "Ok": function() {
                    that = this;
                    
                    /***** Init Query String *****/
                    var queryString = 'id='+jQuery("#pick_up_date_member").val();                    
                    queryString += "&t="+jQuery("#delivery_or_pick_up").val();
                    queryString += "&d="+jQuery("#pick_up_date_obj").val();
                    
                    jQuery.ajax({
                        type: "POST",
                        url: "/administrator/index.php?option=com_wrd&controller=reports&layout=redemptionreport&task=updateDateRedemption",
                        data: queryString,
                    }).success(function( json ) {
                        var result = eval(json);
                        if(result.success == true) {
                            if(jQuery("#delivery_or_pick_up").val() == 1) {
                                jQuery("#pick-up-date_"+jQuery("#pick_up_date_member").val()).find("span").html(result.html);    
                            } else {
                                jQuery("#deliver-to-outlet-date_"+jQuery("#pick_up_date_member").val()).find("span").html(result.html);
                            }
                            
                        }
                        jQuery(that).dialog("close");
                    });
                },
                "Cancel": function() {
                    jQuery(this).dialog("close");
                }
            }
        });
        jQuery('.pick-up-date').click(function(){
            var idMember = jQuery(this).attr("name").split("_");
            jQuery("#pick_up_date_member").val(idMember[1]);
            jQuery("#delivery_or_pick_up").val(1);
            jQuery('#dialog').dialog('open');
            return false;
        });
        
        jQuery('.deliver_to_outlet-date').click(function(){
            var idMember = jQuery(this).attr("name").split("_");
            jQuery("#pick_up_date_member").val(idMember[1]);
            jQuery("#delivery_or_pick_up").val(2);            
            jQuery('#dialog').dialog('open');
            return false;
        });

    });
</script>
<style>
.extra-adminlist strong { color:#025A8D; font-size: 1.2em; font-weight: bold; }
table.extra-adminlist .extra-adminlist-tr td {background: #F7F7F7; }
.filter-detail { border-bottom: 1px solid #CCC; padding-bottom: 3px; font-size: 1.4em; }
.filter-detail span { color: #e72222; }
</style>
<?php 
    $complainType = JHTMLwrd::listComplainTypeRaw();
    $mealPeriod = JHTMLwrd::listMealPeriodRaw();
?>
<script type="text/javascript">
jQuery(function(){
    jQuery("#statistic_by").html(jQuery("#statistic_by").html() + "<span style='color:#000;'>Products:</span> " + getSelectedOptionFilter("extra_products")); 
    jQuery("#statistic_by").html(jQuery("#statistic_by").html() + ". <span style='color:#000;'>Outlet:</span> " + getSelectedOptionFilter("pick_up_at"));     
});

function getSelectedOptionFilter(listName)
{
    var html = "";
    jQuery("#"+listName+" option[selected=selected]").each(function(){
        html = html + jQuery(this).html() + ", ";
    });
    return html.substring(0, html.length-2);
}
</script>
<h3><span id="statistic_by"></span><span style="color:#C00; float:right;">Total: <?php echo $this->pagination->total; ?></span></h3>
<table class="adminlist extra-adminlist table">
  <thead>
    <tr class="extra-adminlist-tr" >
        <th nowrap="nowrap"><strong>Code</strong></th>        
        <th nowrap="nowrap"><strong>Member ID</strong></th> 
        <th nowrap="nowrap"><strong>Name</strong></th>        
        <th nowrap="nowrap"><strong>Phone</strong></th>
        
        <th nowrap="nowrap"><strong>Gift Name</strong></th>                   
        <th nowrap="nowrap"><strong>Quantity</strong></th>        
        <th nowrap="nowrap"><strong>Points</strong></th>  
        <th nowrap="nowrap"><strong>Cash</strong></th>
        <th nowrap="nowrap"><strong>Outlet</strong></th>
         
        <th nowrap="nowrap"><strong>Delivery</strong></th> 
        <th nowrap="nowrap"><strong>Pick Up Date</strong></th> 
        <th nowrap="nowrap"><strong>Status</strong></th>      
    </tr>
  </thead>
  <tbody>
    <?php 
        
        if(!empty($this->items)):
        {
            $rIds = array();
            $rId = '';
            $previousCode = '';
            $index = '';
            foreach ($this->items as $key => $item):
            
                if($previousCode == ''):    
                    $previousCode = $item->code;
                endif;
                
                if($item->code != $previousCode):
                    if(count($rIds) <= 0):
                        $rIds[0] = trim($rId, ',');
                    else:
                        $rIds[$index] = trim($rId, ',');
                    endif;
                    
                    $rId = '';
                    $index = '';
                endif;
                
                if($index == ''):
                    $index = $key;
                endif;
                $rId .= $item->id.',';    
                $previousCode = $item->code;
            
            endforeach;
            $rIds[$index] = trim($rId, ',');
        }
        endif;
     ?>
    <?php 
    	$lang =& JFactory::getLanguage();
    	
    	
        if(!empty($this->items)):
            $previousCode = ''; 
            $bg = 1; 
            $index = 0;
            
            foreach($this->items as $key => $item):
            	$name = unserialize($item->product);
            	$name = $name[$lang->getTag()];
                if($item->code != $previousCode):
                    $bg = ($bg == 0 ? 1 : 0);
                endif;
                
                echo '<tr class="row'.($bg).'" valign="top">';
                    echo '<td  style=""><a title="Click to edit redemption details" target="_blank" href="'.JURI::root().'/administrator/index.php?option=com_wrd&controller=profile&layout=updateredemption&id='.$item->profile_id.'&uid='.$item->user_id.'&code='.$item->code.'">'.($item->code != $previousCode ? $item->code : '').'</a>&nbsp;</td>';
                    echo '<td  style=""><a title="Click to view redemptions" target="_blank" href="index.php?option=com_wrd&controller=profile&id='.$item->profile_id.'&layout=form&tab=redemption">'.($item->code != $previousCode ? $item->member_id : '&nbsp;').'</a></td>';   
                    echo '<td style=""><a title="Click to view profile" target="_blank" href="index.php?option=com_wrd&controller=profile&id='.$item->profile_id.'&layout=form&tab=card_registration_information">'.($item->code != $previousCode ? $item->full_name : '&nbsp;').'</a></td>';   
                    echo '<td   style="">'.($item->code != $previousCode ? $item->phone : '&nbsp;').'</td>';
                    echo '<td  style="width:10%">'.$name.'</td>';   
                    echo '<td style="text-align:right;">'.$item->quantity.'</td>';
                    
                    
                    echo '<td  style="text-align:right;">'.(intval($item->point) + intval($item->pointx)).'</td>';   
                    echo '<td  style="text-align:right;">'.$item->cash.'</td>';
                    echo '<td  style="">'.($item->code != $previousCode ? $item->outlet : '&nbsp;').'</td>';   
                    
                    $date = explode(' ', $item->status_of_redemption_date);
                    $date[0] = '<a href="#" name="deliver-to-outlet-date_'.$item->id.'" id="deliver-to-outlet-date_'.$item->id.'" class="deliver_to_outlet-date"><span>'.($date[0] != '' ? $date[0].' '.$date[1] : '0000-00-00').'</span> <img alt="Deliver to Outlet Date" src="/administrator/templates/bluestork/images/system/calendar.png"></a>';
                    echo '<td nowrap="nowrap" style="">'.($item->code != $previousCode ? $date[0] : '&nbsp;').'</td>';
                    
                    $date = explode(' ', $item->pick_up_date);
                    $date[0] = '<a href="#" name="pick-up-date_'.$item->id.'" id="pick-up-date_'.$item->id.'" class="pick-up-date"><span>'.($date[0] != '' ? $date[0].' '.$date[1] : '0000-00-00').'</span> <img alt="Pick Up Date" src="/administrator/templates/bluestork/images/system/calendar.png"></a>';
                    echo '<td nowrap="nowrap" style="">'.($item->code != $previousCode ? $date[0] : '&nbsp;').'</td>';
                    if($item->approved == 1):
                        $item->approved = 'Approve';
                    elseif($item->approved == 0):
                        $item->approved = 'Waiting';
                    elseif($item->approved == 2):
                        $item->approved = 'UnApprove';        
                    endif;
                    echo '<td  style="">'.($item->code != $previousCode ? $item->approved : '&nbsp;').'</td>';   
                echo '</tr>';
                
                $previousCode = $item->code;
                
            endforeach;
        endif;
    ?>
  </tbody>
  <tfoot>
        <tr>
            <td colspan="16">
                <?php
                     echo $this->pagination->getListFooter();
                ?>
            </td>
        </tr>
    </tfoot>
</table>
<!-- ui-dialog -->
<div id="dialog" title="Update Gift Redemption" style="display: none">
    <div class="dialog-content">
        <div class="dialog-pick-up-date">
            <?php
                echo JHTML::_ ( 'calendar', date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y'))), 'pick_up_date_obj', 'pick_up_date_obj', '%Y-%m-%d');
            ?>
            <input type="hidden" id="pick_up_date_member" name="pick_up_date_member" value="" />
            <input type="hidden" id="delivery_or_pick_up" name="delivery_or_pick_up" value="" />
        </div>
    </div>
</div>
